/**
 * el-table配置高度属性后，超出高度部分循环滚动指令
 * 页面销毁后定时器一起销毁
 * 默认配置
 * @eg <el-table v-scroll />
 * 参数配置
 * <el-table v-scroll="{move: 1, time: 1000, box: '.el-table__body-wrapper .el-scrollbar__wrap'}" />
 * 参数对象依次为：move每次移动的像素，time移动的时间频率1000=1秒，box移动的容器类名
 * box移动的容器类名配置不正确会导致指令无法正常轮播滚动
 */
export default {
  mounted(el: any, binding: any) {
    timer(el, binding)
  },
  unmounted(el: any, binding: any) {
    clearInterval(el.timerObj)
    el.timerObj = null
  }
}
function timer(el: any, binding: any) {
  const box = binding.value?.box || '.el-table__body-wrapper .el-scrollbar__wrap'
  const divData = el.querySelector(box)
  if (el.timerObj) {
    clearInterval(el.timerObj)
  }
  // 每次移动像素
  const move = binding.value?.move || 1
  // 定时器触发频率1000=1秒
  const time = binding.value?.time || 1000
  el.timerObj = setInterval(() => {
    divData.scrollTop += move
    // 判断元素是否滚动到底部(可视高度+距离顶部=整个高度)
    if (divData.clientHeight + divData.scrollTop >= divData.scrollHeight - 1) {
      // 重置table距离顶部距离
      divData.scrollTop = 0
    }
  }, time) // 滚动速度
}